package config.table.xls;
import java.io.File;
import java.util.ArrayList;

public class ConfigTableFile 
{
	public static final ConfigTableFile Instance = new ConfigTableFile();

	private ArrayList<File> fileList;
	
	public void init()
	{
		this.fileList = getAllXlsFile();
	}
	
	private ArrayList<File> getAllXlsFile()
	{
		ArrayList<File> fileList = new ArrayList<File>();
		getXlsFile(fileList , Parameters.Instance.getXlsDir());
		return fileList;
	}
	
	private void getXlsFile(ArrayList<File> fileList, String path) 
	{
		File[] allFiles = new File(path).listFiles();
        for (int i = 0; i < allFiles.length; i++) 
        {
            File file = allFiles[i];
            String fileName = file.getName();
            if(fileName.startsWith("~"))
            	continue;
            
            if(Parameters.Instance.getXlsTables() != null)
            {
            	if(isAppointTable(fileName))
            	{
            		if(file.isDirectory())
                		getXlsFile(fileList, file.getAbsolutePath());
                	else if (file.isFile() && file.getName().lastIndexOf(".xlsx") >= 0)
                		fileList.add(file);
            	}
            }
            else
            {
            	if(file.isDirectory())
            		getXlsFile(fileList, file.getAbsolutePath());
            	else if (file.isFile() && fileName.lastIndexOf(".xlsx") >= 0)
            		fileList.add(file);
            }
        }
	}
	
	private boolean isAppointTable(String fileName)
	{
		for (String appointFileName : Parameters.Instance.getXlsTables()) 
		{
			if(fileName.equals(appointFileName))
				return true;
		}
		return false;
	}

	public ArrayList<File> getFileList() {		return fileList; 	}
}
